//
//  BCOrderHistoryDetailViewController.m
//  BangcaiBuy
//
//  Created by Liang Zhang on 16/7/2.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCOrderHistoryDetailViewController.h"
#import "BCOrderHistoryDetailCollectionViewCell.h"
#import "BCGaryNullCollectionViewCell.h"
#import "BCOrderHistoryStuffCollectionViewCell.h"
#import "BCOrderHistoryAppealViewController.h"

@interface BCOrderHistoryDetailViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) UIButton *cashButton;
@property (nonatomic, strong) UILabel *priceLable;
@end

@implementation BCOrderHistoryDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _isCash = YES;
    [self p_initNavgationBar];
    [self p_initCollectionView];
    [self p_initUnderView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[BCShopCarManager sharedInstance] hideShopWindow];
    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

#pragma mark - Private Action
-(void)cashAction:(UIButton *)sender {
    NSLog(@"cash");
}

#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"订单详情";
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mainCollectionView.contentInset = UIEdgeInsetsMake(0, 0, SCREEN_HEIGHT*0.07f+self.navigationController.navigationBar.frame.size.height+20.f, 0);
    [self.view addSubview:self.mainCollectionView];
}

-(void)p_initUnderView {
    UIView *underbar = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT*0.93f-self.navigationController.navigationBar.frame.size.height-20.f, SCREEN_WIDTH, SCREEN_HEIGHT*0.07f)];
    underbar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:underbar];
    
    UIImageView *topLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索-间隔线"]];
    topLineImageView.frame = CGRectMake(0, -2, SCREEN_WIDTH, 4.f);
    [underbar addSubview:topLineImageView];
    
    if (_isCash) {
        _priceLable = [[UILabel alloc] initWithFrame:CGRectMake(8.f, (underbar.frame.size.height-36.f)*0.5f, underbar.frame.size.width-16.f, 36.f)];
        NSString *str = @"165".rmbString;
        NSString *priceString = [NSString stringWithFormat:@"总价：%@",str];
        _priceLable.textAlignment = NSTextAlignmentRight;
        NSMutableAttributedString *priceAttributedString = [[NSMutableAttributedString alloc] initWithString:priceString];
        UIColor *titleColor = [UIColor lightGrayColor];
        UIColor *rmbColor = [UIColor colorWithRed:0.933 green:0.314 blue:0.188 alpha:1.000];
        [priceAttributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
                                    value:titleColor
                                    range:NSMakeRange(0, @"总价：".length)];
        [priceAttributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
                                    value:rmbColor
                                    range:NSMakeRange(@"总价：".length, str.length)];
        UIFont *titleFont = [UIFont systemFontOfSize:15];
        UIFont *rmbFont = [UIFont systemFontOfSize:20];
        if (SCREEN_WIDTH<=320) {
            titleFont = [UIFont systemFontOfSize:10];
            rmbFont = [UIFont systemFontOfSize:15];
        }
        [priceAttributedString addAttribute:NSFontAttributeName             //文字字体
                                    value:titleFont
                                    range:NSMakeRange(0, @"总价：".length)];
        [priceAttributedString addAttribute:NSFontAttributeName             //文字字体
                                    value:rmbFont
                                    range:NSMakeRange(@"总价：".length, str.length)];
        _priceLable.attributedText = priceAttributedString;
        [underbar addSubview:_priceLable];
    }
    else {
        _cashButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cashButton.frame = CGRectMake(SCREEN_WIDTH-96.f-8.f, (underbar.frame.size.height-36.f)*0.5f, 96.f, 36.f);
        _cashButton.backgroundColor = [UIColor colorWithRed:0.953 green:0.557 blue:0.192 alpha:1.000];
        _cashButton.layer.cornerRadius = 2.f;
        [_cashButton setTitle:@"去结算" forState:UIControlStateNormal];
        [_cashButton addTarget:self action:@selector(cashAction:) forControlEvents:UIControlEventTouchUpInside];
        [underbar addSubview:_cashButton];
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 8;
    }
    else if (section == 1){
        return 1;
    }
    else {
        return 4;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            static NSString *ID = @"HistoryDetailCollectionViewCell";
            BCOrderHistoryDetailCollectionViewCell *cell = [BCOrderHistoryDetailCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.detailLable.font = [UIFont systemFontOfSize:14];
            cell.detailLable.textColor = [UIColor darkTextColor];
            cell.detailLable.text = [NSString stringWithFormat:@"订单编号：%@",@"0160620411030005"];
            return cell;
        }
        else if (indexPath.row == 1) {
            static NSString *ID = @"HistoryDetailCollectionViewCell";
            BCOrderHistoryDetailCollectionViewCell *cell = [BCOrderHistoryDetailCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.detailLable.font = [UIFont systemFontOfSize:12];
            cell.detailLable.textColor = [UIColor lightGrayColor];
            cell.detailLable.text = [NSString stringWithFormat:@"下单时间：%@",@"2016-6-20 15:00"];
            return cell;
        }
        else if (indexPath.row == 2) {
            static NSString *ID = @"HistoryDetailCollectionViewCell";
            BCOrderHistoryDetailCollectionViewCell *cell = [BCOrderHistoryDetailCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.detailLable.font = [UIFont systemFontOfSize:12];
            cell.detailLable.textColor = [UIColor lightGrayColor];
            cell.detailLable.text = [NSString stringWithFormat:@"支付方式：%@",@"支付宝"];
            return cell;
        }
        else if (indexPath.row == 3) {
            static NSString *ID = @"HistoryDetailCollectionViewCell";
            BCOrderHistoryDetailCollectionViewCell *cell = [BCOrderHistoryDetailCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.detailLable.font = [UIFont systemFontOfSize:12];
            cell.detailLable.textColor = [UIColor lightGrayColor];
            cell.detailLable.text = [NSString stringWithFormat:@"收货地址：%@",@"大连市沙河口区中山路604号"];
            return cell;
        }
        else if (indexPath.row == 4) {
            static NSString *ID = @"HistoryDetailCollectionViewCell";
            BCOrderHistoryDetailCollectionViewCell *cell = [BCOrderHistoryDetailCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.detailLable.font = [UIFont systemFontOfSize:12];
            cell.detailLable.textColor = [UIColor lightGrayColor];
            cell.detailLable.text = [NSString stringWithFormat:@"下单时间：%@",@"2016-6-20 15:00"];
            return cell;
        }
        else if (indexPath.row == 5) {
            static NSString *ID = @"HistoryDetailCollectionViewCell";
            BCOrderHistoryDetailCollectionViewCell *cell = [BCOrderHistoryDetailCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.detailLable.font = [UIFont systemFontOfSize:12];
            cell.detailLable.textColor = [UIColor lightGrayColor];
            cell.detailLable.text = [NSString stringWithFormat:@"收货人员：%@",@"刘先生 158****3188"];
            return cell;
        }
        else if(indexPath.row == 6){
            static NSString *ID = @"HistoryDetailCollectionViewCell";
            BCOrderHistoryDetailCollectionViewCell *cell = [BCOrderHistoryDetailCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.detailLable.font = [UIFont systemFontOfSize:12];
            cell.detailLable.textColor = [UIColor lightGrayColor];
            cell.detailLable.text = [NSString stringWithFormat:@"配送方式：%@",@"送货上门"];
            return cell;
        }
        else {
            static NSString *ID = @"MineGaryNullCell";
            BCGaryNullCollectionViewCell *cell = [BCGaryNullCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }

    }
    else if (indexPath.section == 1){
        static NSString *ID = @"HistoryDetailCollectionViewCell";
        BCOrderHistoryDetailCollectionViewCell *cell = [BCOrderHistoryDetailCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        cell.detailLable.font = [UIFont systemFontOfSize:15];
        cell.detailLable.textColor = [UIColor darkTextColor];
        cell.detailLable.text = @"商品详情";
        return cell;
    }
    else {
        static NSString *ID = @"HistoryStuffCollectionViewCell";
        BCOrderHistoryStuffCollectionViewCell *cell = [BCOrderHistoryStuffCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (!_isCash) {
            cell.appealButton.hidden = YES;
        }
        cell.appeal = ^() {
            //申诉
            BCOrderHistoryAppealViewController *nextController = [[BCOrderHistoryAppealViewController alloc] init];
            [self.navigationController pushViewController:nextController animated:YES];
        };
        return cell;
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
//    if (indexPath.section == 0) {
//    }

    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 7) {
            return CGSizeMake(self.view.frame.size.width, 5);
        }
        else if (indexPath.row == 0) {
            return CGSizeMake(self.view.frame.size.width, 36);
        }
        else {
            return CGSizeMake(self.view.frame.size.width, 28);
        }
    }
    else if (indexPath.section == 1) {
        return CGSizeMake(self.view.frame.size.width, 44);
    }
    else {
        return CGSizeMake(self.view.frame.size.width, 150);
    }
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    else if (section == 1) {
        return 0;
    }
    else {
        return 0;
    }
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    else if (section == 1) {
        return 0;
    }
    else {
        return 0;
    }
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

@end
